package com.richapp.launcher.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.nfc.Tag;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.richapp.launcher.R;
import com.richapp.launcher.common.AdvType;
import com.richapp.launcher.dao.adv.Adv;
import com.richapp.launcher.service.AdvService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by yif on 16/8/15.
 */
public class ScreenOnAdvView extends View {

    private static final String TAG = ScreenSaverPicView.class.getName();

    public static volatile Boolean needUpdateResources = false;

    public final String PATH_ADV_FILE = getContext().getFilesDir() + File.separator + "adv";

    int COMPONENT_WIDTH;//控件的宽度

    int COMPONENT_HEIGHT;//控件的高度

    boolean initflag = false;//是不是已经初始化图片

    List<Bitmap> bitmaps = new ArrayList<Bitmap>();

    boolean workFlag = true;//播放图片的线程标识位


    List<Adv> advs = AdvService.advMap.get(AdvType.SCREEN_ON_TYPE);

    public ScreenOnAdvView(Context father, AttributeSet as) {

        super(father);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    initBitmap();
                    Log.i(TAG, bitmaps.size() + " - " + advs.size());
                    if (bitmaps.isEmpty())
                        return;
                    ScreenOnAdvView.this.postInvalidate();//刷新屏幕，导致屏幕重绘
                    if (advs.size() > 0)
                        AdvService.playedAdvs.offer(advs.get(0));
                } catch (Exception e) {
                    Log.e(ScreenSaverPicView.class.getName(), "播放广告失败", e);
                }

            }
        }).start();

    }

    //初始化图片
    public void initBitmap() {

        bitmaps.clear();

        BitmapFactory.Options options = new BitmapFactory.Options();

        advs = AdvService.advMap.get(AdvType.SCREEN_ON_TYPE);

        Log.e(TAG, "advs size: " + advs.size());

        for (Adv adv : advs) {
            String id = adv.getId();
            try {
                Bitmap bitmap = BitmapFactory.decodeFile(PATH_ADV_FILE + File.separator + id, options);
                if (bitmap != null)
                    bitmaps.add(bitmap);
            } catch (Exception e) {
                Log.e(ScreenSaverPicView.class.getName(), "加载图片失败 id=" + id, e);
            }
        }

        int size = 1 - bitmaps.size();
        for (int i = 0; i < size; i++) { //加载默认资源,从0到size-1
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.screen_on, options);
            bitmaps.add(bitmap);
        }
        Log.e(TAG, "bitmaps size: " + bitmaps.size());
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!initflag)//检查是偶已经获得控件的宽和高，如果没有，那么就获得控件的宽和高
        {
            COMPONENT_WIDTH = this.getWidth();
            COMPONENT_HEIGHT = this.getHeight();
            initflag = true;
        }
        RectF rectF = new RectF(0, 0, COMPONENT_WIDTH, COMPONENT_HEIGHT);
        canvas.drawBitmap(bitmaps.get(0), null, rectF, null);//绘制图片

    }
}
